from .base_champion import Champion
from typing import Dict, List, Optional
import random


class ThunderMage(Champion):
    def __init__(self):
        super().__init__(
            name="Thunder Mage",
            health=80,
            attack=35,
            defense=8,
            speed=20,
            mana=150
        )
    
    def get_abilities(self) -> List[str]:
        return ["Lightning Bolt", "Thunder Storm", "Electric Shield", "Lightning Chain"]
    
    def use_ability(self, ability_name: str, target: Optional[Champion] = None) -> Dict:
        if not self._is_alive:
            return {"success": False, "message": "Cannot use ability - champion is dead"}
        
        if self._is_silenced:
            return {"success": False, "message": f"{self.name} is silenced and cannot use abilities"}
        
        if ability_name == "Lightning Bolt":
            return self._lightning_bolt(target)
        elif ability_name == "Thunder Storm":
            return self._thunder_storm()
        elif ability_name == "Electric Shield":
            return self._electric_shield()
        elif ability_name == "Lightning Chain":
            return self._lightning_chain(target)
        else:
            return {"success": False, "message": f"Unknown ability: {ability_name}"}
    
    def _lightning_bolt(self, target: Optional[Champion]) -> Dict:
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(30):
            return {"success": False, "message": "Not enough mana"}
        damage = int(self.attack * 1.4)
        actual_damage = target.take_damage(damage, "magical")
        
        if random.random() < 0.2:
            target._is_stunned = True
        
        return {
            "success": True,
            "damage": actual_damage,
            "message": f"{self.name} casts Lightning Bolt on {target.name} for {actual_damage} damage!"
        }
    
    def _thunder_storm(self) -> Dict:
        if not self.consume_mana(50):
            return {"success": False, "message": "Not enough mana"}
        
        return {
            "success": True,
            "message": f"{self.name} summons a Thunder Storm! (Area effect - damages all enemies)"
        }
    
    def _electric_shield(self) -> Dict:
        if not self.consume_mana(25):
            return {"success": False, "message": "Not enough mana"}
        
        self._buffs.append({
            "type": "electric_shield",
            "damage_reflect": 15,
            "duration": 4
        })
        
        return {
            "success": True,
            "message": f"{self.name} creates an Electric Shield that reflects 15 damage for 4 turns!"
        }
    
    def _lightning_chain(self, target: Optional[Champion]) -> Dict:
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(40):
            return {"success": False, "message": "Not enough mana"}
        
        damage = int(self.attack * 1.2)
        actual_damage = target.take_damage(damage, "magical")
        
        return {
            "success": True,
            "damage": actual_damage,
            "message": f"{self.name} casts Lightning Chain on {target.name} for {actual_damage} damage! (Chains to nearby enemies)"
        }
    
    def get_champion_type(self) -> str:
        return "Mage"


class IceSorcerer(Champion):
    def __init__(self):
        super().__init__(
            name="Ice Sorcerer",
            health=85,
            attack=30,
            defense=10,
            speed=18,
            mana=140
        )
    
    def get_abilities(self) -> List[str]:
        return ["Ice Shard", "Blizzard", "Ice Armor", "Frozen Prison"]
    
    def use_ability(self, ability_name: str, target: Optional[Champion] = None) -> Dict:
        if not self._is_alive:
            return {"success": False, "message": "Cannot use ability - champion is dead"}
        
        if self._is_silenced:
            return {"success": False, "message": f"{self.name} is silenced and cannot use abilities"}
        
        if ability_name == "Ice Shard":
            return self._ice_shard(target)
        elif ability_name == "Blizzard":
            return self._blizzard()
        elif ability_name == "Ice Armor":
            return self._ice_armor()
        elif ability_name == "Frozen Prison":
            return self._frozen_prison(target)
        else:
            return {"success": False, "message": f"Unknown ability: {ability_name}"}
    
    def _ice_shard(self, target: Optional[Champion]) -> Dict:
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(20):
            return {"success": False, "message": "Not enough mana"}
        
        damage = int(self.attack * 1.1)
        actual_damage = target.take_damage(damage, "magical")
        
        if random.random() < 0.3:
            target._debuffs.append({
                "type": "slow",
                "speed_reduction": 5,
                "duration": 2
            })
        
        return {
            "success": True,
            "damage": actual_damage,
            "message": f"{self.name} hurls an Ice Shard at {target.name} for {actual_damage} damage!"
        }
    
    def _blizzard(self) -> Dict:
        if not self.consume_mana(60):
            return {"success": False, "message": "Not enough mana"}
        
        return {
            "success": True,
            "message": f"{self.name} summons a Blizzard! (Area effect - damages and slows all enemies)"
        }
    
    def _ice_armor(self) -> Dict:
        if not self.consume_mana(30):
            return {"success": False, "message": "Not enough mana"}
        
        self._buffs.append({
            "type": "ice_armor",
            "defense_boost": 15,
            "damage_reduction": 20,
            "duration": 5
        })
        
        return {
            "success": True,
            "message": f"{self.name} creates Ice Armor, gaining +15 defense and 20% damage reduction for 5 turns!"
        }
    
    def _frozen_prison(self, target: Optional[Champion]) -> Dict:
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(35):
            return {"success": False, "message": "Not enough mana"}
        
        target._is_stunned = True
        target._debuffs.append({
            "type": "frozen",
            "duration": 2
        })
        
        return {
            "success": True,
            "message": f"{self.name} traps {target.name} in a Frozen Prison for 2 turns!"
        }
    
    def get_champion_type(self) -> str:
        return "Mage"


class FireWarlock(Champion):
    def __init__(self):
        super().__init__(
            name="Fire Warlock",
            health=90,
            attack=40,
            defense=8,
            speed=16,
            mana=120
        )
    
    def get_abilities(self) -> List[str]:
        return ["Fireball", "Inferno", "Fire Shield", "Meteor Strike"]
    
    def use_ability(self, ability_name: str, target: Optional[Champion] = None) -> Dict:
        if not self._is_alive:
            return {"success": False, "message": "Cannot use ability - champion is dead"}
        
        if self._is_silenced:
            return {"success": False, "message": f"{self.name} is silenced and cannot use abilities"}
        
        if ability_name == "Fireball":
            return self._fireball(target)
        elif ability_name == "Inferno":
            return self._inferno()
        elif ability_name == "Fire Shield":
            return self._fire_shield()
        elif ability_name == "Meteor Strike":
            return self._meteor_strike(target)
        else:
            return {"success": False, "message": f"Unknown ability: {ability_name}"}
    
    def _fireball(self, target: Optional[Champion]) -> Dict:
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(25):
            return {"success": False, "message": "Not enough mana"}
        
        damage = int(self.attack * 1.3)
        actual_damage = target.take_damage(damage, "magical")
        
        target._debuffs.append({
            "type": "burn",
            "damage_per_turn": 5,
            "duration": 3
        })
        
        return {
            "success": True,
            "damage": actual_damage,
            "message": f"{self.name} hurls a Fireball at {target.name} for {actual_damage} damage! (Burning effect applied)"
        }
    
    def _inferno(self) -> Dict:
        if not self.consume_mana(70):
            return {"success": False, "message": "Not enough mana"}
        
        return {
            "success": True,
            "message": f"{self.name} creates an Inferno! (Area effect - massive fire damage to all enemies)"
        }
    
    def _fire_shield(self) -> Dict:
        if not self.consume_mana(20):
            return {"success": False, "message": "Not enough mana"}
        
        self._buffs.append({
            "type": "fire_shield",
            "damage_reflect": 10,
            "burn_chance": 0.5,
            "duration": 4
        })
        
        return {
            "success": True,
            "message": f"{self.name} creates a Fire Shield that burns attackers for 4 turns!"
        }
    
    def _meteor_strike(self, target: Optional[Champion]) -> Dict:
        """Devastating meteor attack"""
        if not target or not target.is_alive:
            return {"success": False, "message": "No valid target"}
        
        if not self.consume_mana(50):
            return {"success": False, "message": "Not enough mana"}
        
        damage = int(self.attack * 2.0)  
        actual_damage = target.take_damage(damage, "magical")
        
        return {
            "success": True,
            "damage": actual_damage,
            "message": f"{self.name} calls down a Meteor Strike on {target.name} for {actual_damage} damage!"
        }
    
    def get_champion_type(self) -> str:
        return "Mage"

